package com.qm.console.service.withdraw.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qm.console.component.util.HttpInvokeUtil;
import com.qm.console.controller.dto.BaseRsp;
import com.qm.console.dao.mapper.IllegalRecordDOMapper;
import com.qm.console.dao.model.IllegalRecordDO;
import com.qm.console.pubdef.constant.UrlConstant;
import com.qm.console.pubdef.dto.withdraw.WithdrawUpdateAdminDTO;
import com.qm.console.service.withdraw.WithdrawService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class WithdrawServiceImpl implements WithdrawService {
    private final String withdrawAdminUrl="/withdraw";

    @Autowired
    private IllegalRecordDOMapper illegalRecordDOMapper;

    @Override
    public BaseRsp listWithdrawAdmin(Map<String,String> paramMap){
        String url= UrlConstant.ApiServerHost+withdrawAdminUrl+"/batch";
        String body= HttpInvokeUtil.httpGet(url,paramMap, null);
        BaseRsp rsp= JSONObject.parseObject(body,BaseRsp.class);
        Object rspData=rsp.getData();
        final JSONObject data=(JSONObject) rspData;

        ArrayList<String> userIds = new ArrayList<>();

        if(data != null && data.containsKey("userWithdrawList")){
            JSONArray userWithdrawList= data.getJSONArray("userWithdrawList");

            userWithdrawList.forEach(item->{
                JSONObject temp = JSON.parseObject(item.toString());
                userIds.add(temp.getInteger("userId").toString());
            });
            Map<Integer, Object> userInfo = new HashMap<Integer, Object>();
            if (userIds.size() > 0){
                String uids = String.join(",", userIds);
                List<IllegalRecordDO> illegalInfo = illegalRecordDOMapper.userIllegalCount(uids);
                illegalInfo.forEach(item -> {
                    userInfo.put(item.getUid(), item);
                });
            }
            for(int i=0;i<userWithdrawList.size();i++){
                JSONObject item=userWithdrawList.getJSONObject(i);
                String withDrawId=item.getString("withDrawId");
                item.put("illegalInfo", userInfo.getOrDefault(item.getInteger("userId"), null));
                if(withDrawId!=null){
                    item.put("withDrawId",withDrawId);
                }
            }
        }

        return  rsp;
    }

    @Override
    public BaseRsp updateWithdrawAdmin(WithdrawUpdateAdminDTO updateDTO){
        Map dealUserWithDrawReq=new HashMap<>();
        Boolean result=updateDTO.getResult();
        if(result!=null){
            dealUserWithDrawReq.put("result",result);
        }
        String[] withDrawId=updateDTO.getWithDrawId();
        if(withDrawId!=null){
            dealUserWithDrawReq.put("withDrawId",withDrawId);
        }
        String url=UrlConstant.ApiServerHost+withdrawAdminUrl+"/deal";
        String req=JSONObject.toJSONString(updateDTO);
        String body=HttpInvokeUtil.httpPostJson(url,req,UrlConstant.DefaultEncode,null);
        BaseRsp rsp=JSONObject.parseObject(body,BaseRsp.class);
        return rsp;
    }
}
